﻿#include "event_proc.h"
#include "adapter.h"
#include "typedef.h"
#include "plugin_info_inl.c"

#include <stdio.h>

Adapter_ApiParameterStruct gApiParam;

DWORD32 EventCallback(PAdapter_EventParameterStruct pEventParameterStruct) {
    switch (pEventParameterStruct->eventType) {
        case Adapter_EventTypePluginLoad: {
            gApiParam.authCode = Adapter_Initialize(pEventParameterStruct->session, infoJsonStr);
            return gApiParam.authCode;
        }
        case Adapter_EventTypeInit: {
            Adapter_CallApi("SetFatal", &gApiParam);
            gApiParam.msg = "C++ Adapter EventInit";
            Adapter_CallApi("AppendLogs", &gApiParam);
            break;
        }
        case Adapter_EventTypeFriendMsg: {
            if (pEventParameterStruct->messageType == MSG_TYPE_TEXT) {
                CHAR buffer[2048];
                snprintf(buffer, sizeof(buffer), "Received message \"%s\" from %s", pEventParameterStruct->msg, pEventParameterStruct->fromName);
                gApiParam.robotWxid = pEventParameterStruct->robotWxid;
                gApiParam.toWxid = pEventParameterStruct->fromWxid;
                gApiParam.msg = buffer;
                Adapter_CallApi("SendTextMsg", &gApiParam);
                Adapter_CallApi("AppendLogs", &gApiParam);
            }
            return RET_TYPE_IGNORE;
        }
    }
    return RET_TYPE_IGNORE;
}

void InitializeAdapter() {
    Adapter_SetEventCallbackFunc(EventCallback);
}
